﻿Imports System.Data
Imports System.Data.SqlClient
'Hacemos referencia al proyecto BusinessEntities
Imports BE = BusinessEntities

Public Class CarritoDAO
    Dim conexion As SqlConnection
    Dim comando As SqlDataAdapter
    Public Shared errores As String
    Dim com As SqlCommand

    'Instanciamos la clase Conexion
    Dim cn As New Conexion



    Public Function ListarCarritos(ByVal objeto As BE.CarritoBE) As DataSet

        Dim ds As New DataSet
        Try
            conexion = cn.Conectar
            comando = New SqlDataAdapter("SP_LISTARCARRITOS", conexion)
            comando.SelectCommand.CommandType = CommandType.StoredProcedure

            comando.Fill(ds, "LosCarritos")

        Catch ex As Exception
            errores = ex.Message
        Finally
            conexion.Dispose()
            comando.Dispose()
            ds.Dispose()
        End Try

        Return ds

    End Function


    Public Function ConsultarCarrito(ByVal objeto As BE.CarritoBE) As DataSet

        Dim ds As New DataSet
        Try
            conexion = cn.Conectar
            comando = New SqlDataAdapter("SP_CONSULTARCARRITO", conexion)
            comando.SelectCommand.CommandType = CommandType.StoredProcedure

            'Definimos el paràmetro del stored procedure
            comando.SelectCommand.Parameters.Add(New SqlParameter("@CODCARRO", SqlDbType.Char, 7))
            comando.SelectCommand.Parameters("@CODCARRO").Direction = ParameterDirection.Input
            comando.SelectCommand.Parameters("@CODCARRO").Value = objeto.codigo

            comando.Fill(ds, "ElCarrito")

        Catch ex As Exception
            errores = ex.Message
        Finally
            conexion.Dispose()
            comando.Dispose()
            ds.Dispose()
        End Try

        Return ds

    End Function


    Public Function LlenarCarrito(ByVal objeto As BE.CarritoBE) As Boolean

        Try
            conexion = cn.Conectar
            com = New SqlCommand("SP_CANTITEMSXCARRITO", conexion)
            com.CommandType = CommandType.StoredProcedure

            'Definimos el paràmetro del stored procedure
            com.Parameters.Add(New SqlParameter("@CARRITO", SqlDbType.Char, 7))
            com.Parameters("@CARRITO").Direction = ParameterDirection.Input
            com.Parameters("@CARRITO").Value = objeto.codigo

            com.Parameters.Add(New SqlParameter("@SERVICIO", SqlDbType.Char, 6))
            com.Parameters("@SERVICIO").Direction = ParameterDirection.Input
            com.Parameters("@SERVICIO").Value = objeto.codigoservicio
            ''Ahora nos aseguramos que para ese carrito un servicio
            ''dado aún no haya sido insertado, es decir, no hay registros
            ''para ese carrito y ese servicio. Esto nos garantiza que
            ''un servicio se inserte una sola vez por mas que el usuario
            ''efectúe varias veces click al botón Añadir al carrito

            conexion.Open()

            If com.ExecuteScalar() = 0 Then
                com.Dispose()

                com = New SqlCommand("SP_INSERTARCARRITO", conexion)
                com.CommandType = CommandType.StoredProcedure

                com.Parameters.Add(New SqlParameter("@CARRITO", SqlDbType.Char, 7))
                com.Parameters("@CARRITO").Direction = ParameterDirection.Input
                com.Parameters("@CARRITO").Value = objeto.codigo

                com.Parameters.Add(New SqlParameter("@SERVICIO", SqlDbType.Char, 6))
                com.Parameters("@SERVICIO").Direction = ParameterDirection.Input
                com.Parameters("@SERVICIO").Value = objeto.codigoservicio

                com.ExecuteNonQuery()

            End If
            Return True
        Catch ex As Exception
            errores = ex.Message
            Return False
        Finally
            If conexion.State = ConnectionState.Open Then conexion.Close()
            'Liberamos recursos
            conexion.Dispose()

        End Try

    End Function

    ' le manda codigo carrito y codigo servicio
    Public Function QuitarItemdelCarrito(ByVal objeto As BE.CarritoBE) As Boolean
        Try
            conexion = cn.Conectar
            com = New SqlCommand("SP_BORRARITEMDELCARRITO", conexion)
            com.CommandType = CommandType.StoredProcedure

            com.Parameters.Add(New SqlParameter("@CARRITO", SqlDbType.Char, 7))
            com.Parameters("@CARRITO").Direction = ParameterDirection.Input
            com.Parameters("@CARRITO").Value = objeto.codigo

            com.Parameters.Add(New SqlParameter("@SERVICIO", SqlDbType.Char, 6))
            com.Parameters("@SERVICIO").Direction = ParameterDirection.Input
            com.Parameters("@SERVICIO").Value = objeto.codigoservicio

            conexion.Open()
            com.ExecuteNonQuery()
            Return True
        Catch ex As Exception
            errores = ex.Message
            Return False
        Finally
            If conexion.State = ConnectionState.Open Then conexion.Close()
            'Liberamos recursos
            conexion.Dispose()
            com.Dispose()
        End Try

    End Function

    Public Function ActualizarItemdelCarrito(ByVal objeto As BE.CarritoBE) As Boolean

        Try
            conexion = cn.Conectar

            com = New SqlCommand("SP_ACTUALIZARITEMDELCARRITO", conexion)
            com.CommandType = CommandType.StoredProcedure

            com.Parameters.Add(New SqlParameter("@CARRITO", SqlDbType.Char, 7))
            com.Parameters("@CARRITO").Direction = ParameterDirection.Input
            com.Parameters("@CARRITO").Value = objeto.codigo

            com.Parameters.Add(New SqlParameter("@SERVICIO", SqlDbType.Char, 6))
            com.Parameters("@SERVICIO").Direction = ParameterDirection.Input
            com.Parameters("@SERVICIO").Value = objeto.codigoservicio

            com.Parameters.Add(New SqlParameter("@CANTIDAD", SqlDbType.Int))
            com.Parameters("@CANTIDAD").Direction = ParameterDirection.Input
            com.Parameters("@CANTIDAD").Value = objeto.cantidad

            conexion.Open()
            com.ExecuteNonQuery()
            Return True
        Catch ex As Exception
            errores = ex.Message
            Return False
        Finally ''la conexion tiene q  ser de una sola Session
            If conexion.State = ConnectionState.Open Then conexion.Close()
            'Liberamos recursos
            conexion.Dispose()
            com.Dispose()
        End Try

    End Function

    Public Function ConsultarItemdelCarrito(ByVal objeto As BE.CarritoBE) As DataSet

        Dim ds As New DataSet
        Try
            conexion = cn.Conectar
            comando = New SqlDataAdapter("SP_CONSULTARITEMDELCARRITO", conexion)
            comando.SelectCommand.CommandType = CommandType.StoredProcedure

            comando.SelectCommand.Parameters.Add(New SqlParameter("@CARRITO", SqlDbType.Char, 7))
            comando.SelectCommand.Parameters("@CARRITO").Direction = ParameterDirection.Input
            comando.SelectCommand.Parameters("@CARRITO").Value = objeto.codigo

            comando.SelectCommand.Parameters.Add(New SqlParameter("@SERVICIO", SqlDbType.Char, 6))
            comando.SelectCommand.Parameters("@SERVICIO").Direction = ParameterDirection.Input
            comando.SelectCommand.Parameters("@SERVICIO").Value = objeto.codigoservicio

            comando.Fill(ds, "ItemdelCarrito")
        Catch ex As Exception
            errores = ex.Message
        Finally
            conexion.Dispose()
            comando.Dispose()
            ds.Dispose()
        End Try

        Return ds

    End Function

    Public Function ConsultarElementosdelCarrito(ByVal objeto As BE.CarritoBE) As DataSet

        Dim ds As New DataSet
        Try
            conexion = cn.Conectar

            comando = New SqlDataAdapter("SP_CONSULTARELEMENTOSCARRITO", conexion)
            comando.SelectCommand.CommandType = CommandType.StoredProcedure

            comando.SelectCommand.Parameters.Add(New SqlParameter("@CODCARRO", SqlDbType.Char, 7))
            comando.SelectCommand.Parameters("@CODCARRO").Direction = ParameterDirection.Input
            comando.SelectCommand.Parameters("@CODCARRO").Value = objeto.codigo

            comando.Fill(ds, "ElementosdelCarrito")
        Catch ex As Exception
            errores = ex.Message
        Finally
            conexion.Dispose()
            comando.Dispose()
            ds.Dispose()
        End Try

        Return ds

    End Function

    Public Function EliminarCarrito(ByVal objeto As BE.CarritoBE) As Boolean

        Try
            conexion = cn.Conectar
            com = New SqlCommand("SP_BORRARCARRITO", conexion)
            com.CommandType = CommandType.StoredProcedure
            com.Parameters.Add(New SqlParameter("@CODCARRO", SqlDbType.Char, 7))
            com.Parameters("@CODCARRO").Direction = ParameterDirection.Input
            com.Parameters("@CODCARRO").Value = objeto.codigo

            conexion.Open()
            com.ExecuteNonQuery()
            Return True
        Catch ex As SqlException
            errores = ex.Message
            Return False
        Finally
            If conexion.State = ConnectionState.Open Then conexion.Close()
            'Liberamos recursos
            conexion.Dispose()
            com.Dispose()
        End Try
    End Function

    Public Function EliminarCarritosPasados() As Boolean
        
        Try
            conexion = cn.Conectar

            com = New SqlCommand("SP_ELIMINARREGISTROSPASADOS", conexion)
            com.CommandType = CommandType.StoredProcedure

            conexion.Open()
            com.ExecuteNonQuery()
            Return True
        Catch ex As SqlException
            errores = ex.Message
            Return False
        Finally
            If conexion.State = ConnectionState.Open Then conexion.Close()
            'Liberamos recursos
            conexion.Dispose()
            com.Dispose()
        End Try
    End Function


    Public Function ChapaError() As String
        Return errores
    End Function


End Class
